﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Common.Model;
using Common.Error;
using OpenAPI.Model.OAuth;
using OpenAPI.IDAL.OAuth;
using OpenAPI.DALFactory.OAuth;

namespace OpenAPI.BLL.OAuth
{
    public class ConsumerManager
    {

        private static readonly IConsumer dal = DataAccess.CreateConsumer();


        #region 消费方管理

        /// <summary>
        /// Consumer register
        /// </summary>
        /// <param name="info"></param>
        /// <param name="error"></param>
        /// <returns></returns>
        public static bool ConsumerRegister(ConsumerInfo info, out ErrorType error)
        {
            return dal.ConsumerRegister(info, out error);
        }


        /// <summary>
        /// 获取消费方信息
        /// </summary>
        /// <param name="oauth_consumer_key"></param>
        /// <param name="oauth_consumer_secret"></param>
        /// <returns></returns>
        public static ConsumerInfo GetConsumer(string oauth_consumer_key, string oauth_consumer_secret)
        {
            return dal.GetConsumer(oauth_consumer_key, oauth_consumer_secret);
        }


        /// <summary>
        /// 消费方清单
        /// </summary>
        /// <returns></returns>
        public static IList<ConsumerInfo> GetConsumers()
        {
            return dal.GetConsumers();
        }

        /// <summary>
        /// 修改消费方
        /// </summary>
        /// <param name="info"></param>
        /// <param name="error"></param>
        /// <returns></returns>
        public static bool ConsumerModify(ConsumerInfo info, out ErrorType error)
        {
            return dal.ConsumerModify(info, out error);
        }

        /// <summary>
        /// 删除消费方
        /// </summary>
        /// <param name="oauth_consumer_key"></param>
        /// <param name="oauth_consumer_secret"></param>
        /// <param name="error"></param>
        /// <returns></returns>
        public static bool DeleteConsumer(string oauth_consumer_key, string oauth_consumer_secret, out ErrorType error)
        {
            return dal.DeleteConsumer(oauth_consumer_key, oauth_consumer_secret, out error);
        }

        #endregion

        #region 令牌、密钥

        /// <summary>
        /// 获取消费方密钥
        /// </summary>
        /// <param name="oauth_consumer_key">消费方键值</param>
        /// <returns></returns>
        public static string GetOAuthConsumerSecret(string oauth_consumer_key, out ErrorType error)
        {
            return dal.GetOAuthConsumerSecret(oauth_consumer_key, out error);
        }

        /// <summary>
        /// 获取令牌密钥
        /// </summary>
        /// <param name="oauth_consumer_key">消费方键值</param>
        /// <param name="oauth_token">令牌</param>
        /// <returns></returns>
        public static string GetOAuthTokenSecret(string oauth_consumer_key, string oauth_token, out ErrorType error)
        {
            return dal.GetOAuthTokenSecret(oauth_consumer_key, oauth_token, out error);
        }


        /// <summary>
        /// 验证Token是否合法
        /// </summary>
        /// <param name="oauth_consumer_key"></param>
        /// <param name="oauth_token"></param>
        /// <returns></returns>
        public static bool ChkOAuthToken(string oauth_consumer_key, string oauth_token)
        {
            return dal.ChkOAuthToken(oauth_consumer_key, oauth_token);
        }


        /// <summary>
        /// 获取请求令牌
        /// </summary>
        /// <param name="oauth_consumer_key">消费方键值</param>
        /// <param name="newToken">新生成的Token</param>
        /// <param name="error"></param>
        /// <returns></returns>
        public static AuthorizeTokenInfo RequestToken(string oauth_consumer_key, AuthorizeTokenInfo newToken, out ErrorType error)
        {
            return dal.RequestToken(oauth_consumer_key, newToken, out error);
        }

        /// <summary>
        /// 验证请求令牌
        /// </summary>
        /// <param name="authToken">欲验证的Token</param>
        /// <param name="error"></param>
        /// <returns></returns>
        public static AuthorizeTokenInfo AuthorizeToken(AuthorizeTokenInfo authToken, out ErrorType error)
        {
            return dal.AuthorizeToken(authToken, out error);
        }


        /// <summary>
        /// 换取访问令牌
        /// </summary>
        /// <param name="oauth_token">令牌</param>
        /// <param name="oauth_token_secret">令牌密钥</param>
        /// <returns></returns>
        public static AuthorizeTokenInfo ExchangeAccessToken(string oauth_token, string oauth_token_secret)
        {
            return dal.ExchangeAccessToken(oauth_token, oauth_token_secret);
        }

        #endregion

        #region 等级

        /// <summary>
        /// 新增消费方等级
        /// </summary>
        /// <param name="level"></param>
        /// <param name="error"></param>
        /// <returns></returns>
        public static bool InsertConsumerLevel(ConsumerLevelInfo level, out ErrorType error)
        {
            return dal.InsertConsumerLevel(level, out error);
        }

        /// <summary>
        /// 更新消费方等级
        /// </summary>
        /// <param name="level"></param>
        /// <param name="error"></param>
        /// <returns></returns>
        public static bool UpdateConsumerLevel(ConsumerLevelInfo level, out ErrorType error)
        {
            return dal.UpdateConsumerLevel(level, out error);
        }

        /// <summary>
        /// 删除消费方等级，同时删除所属方法
        /// </summary>
        /// <param name="consumer_level"></param>
        /// <param name="error"></param>
        /// <returns></returns>
        public static bool DeleteConsumerLevel(string consumer_level, out ErrorType error)
        {
            return dal.DeleteConsumerLevel(consumer_level, out error);
        }

        /// <summary>
        /// 获取消费方等级列表
        /// </summary>
        /// <returns></returns>
        public static IList<ConsumerLevelInfo> GetConsumerLevels()
        {
            return dal.GetConsumerLevels();
        }

        /// <summary>
        /// 获取消费方等级
        /// </summary>
        /// <param name="consumer_level"></param>
        /// <returns></returns>
        public static ConsumerLevelInfo GetConsumerLevel(string consumer_level)
        {
            return dal.GetConsumerLevel(consumer_level);
        }

        /// <summary>
        /// 方法包含在消费方Level中
        /// </summary>
        /// <param name="oauth_consumer_key"></param>
        /// <param name="method_name"></param>
        /// <returns></returns>
        public static bool ContainsMethodInConsumerLevel(string oauth_consumer_key, string method_name)
        {
            return dal.ContainsMethodInConsumerLevel(oauth_consumer_key, method_name);
        }

        #endregion

        #region 消费方等级方法

        /// <summary>
        /// 获取消费方Level中的方法
        /// </summary>
        /// <param name="consumer_level"></param>
        /// <returns></returns>
        public static IList<MethodInConsumerLevelInfo> GetMethodsInConsumerLevel(string consumer_level)
        {
            return dal.GetMethodsInConsumerLevel(consumer_level);
        }

        /// <summary>
        /// 设置消费方Level的方法-保存
        /// </summary>
        /// <param name="consumer_level"></param>
        /// <param name="arr_method"></param>
        /// <param name="error"></param>
        /// <returns></returns>
        public static bool SaveMethodsToConsumerLevel(string consumer_level, string[] arr_method, out ErrorType error)
        {
            return dal.SaveMethodsToConsumerLevel(consumer_level, arr_method, out error);
        }

        #endregion
    }//
}//
